<template>
	<view class="setting">
		<view class="btns">
			<view class="btns-group">
				<button class="btn btn-a" @tap="bindCheckUpdate">检查更新</button>
			</view>
		</view>
		<view class="card card-version fix-bottom">
			当前版本号：{{ app_version }}
		</view>
	</view>
</template>

<script>
	import { getAppVersion } from '@/api/configure.js';
	import { deepClone, mergeUrls, getUserInfo, getBaseImages } from '@/util/util.js';
	import configUrl from '@/util/url.js';

	export default {
		data() {
			return {
				app_version: '',
				app_Local_version: '',
				update_content: '',
				platform: '',
				subDownloadUrl: '',
				jyJPush: []
			}
		},
		methods: {
			async getAppNewVersion() {
				await getAppVersion().then((res) => {
					this.app_version = res.data.app_version;
					this.update_content = res.data.update_content;
					this.subDownloadUrl = res.data.url;
				})
			},
			getLocalAppVersion() {
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					this.app_Local_version = wgtinfo.version;
				});
			},
			updeteApp() {
				let _this = this;
				
				uni.showModal({
					title: "版本更新",
					content: this.update_content, //更新描述
					confirmText: '立即更新',
					cancelText: '稍后进行',
					success: sucRes => {
						if (sucRes.confirm) {
							//如果是安卓直接更新下载
							if (_this.platform === 'android') {
								_this.doUpData();
							} else {

							}
						}
					}
				})
			},
			getPlatform() {
				let _this = this;

				uni.getSystemInfo({
					success: function(e) {
						_this.platform = e.platform
					}
				})
			},
			checkUpdate() {
				if (this.app_Local_version != this.app_version) {
					this.updeteApp();
				} else {
					uni.showToast({
						icon: 'none',
						title: '处置系统：当前已是最新版本'
					})
				}
			},
			doUpData() {
				uni.showLoading({
					title: '更新中……'
				})
				uni.downloadFile({ //执行下载
					url: `${configUrl.Setting}${this.subDownloadUrl}` , //下载地址
					success: data => { //下载成功
						uni.hideLoading();
						if (data.statusCode == 200) {
							uni.showModal({
								title: '',
								content: '更新成功，确定现在安装吗？',
								confirmText: '安装',
								confirmColor: '#EE8F57',
								success: function(res) {
									if (data.statusCode === 200) {
										plus.runtime.install(data.tempFilePath, {
											force: true
										}, function(res) {
											utils.showToast('更新成功，重启中');
											plus.runtime.restart();
										});
									}
								}
							});
						}
					}
				});
			},
			async bindCheckUpdate() {
				await this.getAppNewVersion();

				this.checkUpdate();
			},
			init() {
				this.getAppNewVersion();
				this.getLocalAppVersion();
				this.getPlatform();
			}
		},
		onLoad(option) {
			this.init();
		},
		onReady() {}
	}
</script>

<style lang="scss">
	.setting {
		padding: 60rpx 0rpx 0rpx;
	}

	.btns {
		background-color: #ffffff;

		.btns-group {
			padding: 17rpx 30rpx;
			display: flex;

			.btns-item {
				width: 50%;
				padding: 0rpx 10rpx;
			}
		}

	}

	.btn {
		width: 100%;
		padding: 20rpx 0px;
		font-size: 24rpx;
		line-height: 120%;
		color: #fff;
		text-align: center;
		border: none;
		border-radius: 6rpx;
		background: none;

		&:after {
			display: none;
		}

		&.btn-a {
			background-color: #f29004;
		}
	}

	.card {
		&.card-version {
			padding: 20px;
			font-size: 24rpx;
			line-height: 120%;
			text-align: center;
		}
	}

	.fix-bottom {
		position: fixed;
		bottom: 0rpx;
		left: 0rpx;
		right: 0rpx;
	}
</style>
